package action.country;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.Uploader;
import model.dao.CountryModel;
import model.dto.Country;
import controller.Action;
import controller.ActionForward;

public class ActionAddCountry implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Country dto=new Country();
		CountryModel dao=new CountryModel();
		Uploader up=new Uploader(request, "upload"+File.separator+"country");
		int new_index=dao.getLastIndex()+1;
		boolean hasImageFile=up.upload( ""+new_index);
		dto.setName(up.getMutilPathRequest().getParameter("country_name"));
		
		if(hasImageFile){
				dto.setImage(up.getPreFix()+new_index+"."+up.getExtension());//name as sequence
		}else{
			dto.setImage("default.jpg");//old name
		}
		
		
		if(dao.insert(dto))
			System.out.println("Inserted succesfully.");
		else
			System.out.println("Fail inserting!!!");
		
		ActionForward forward=new ActionForward();
		forward.setPath("country.sp");
		forward.setRedirect(true);
		return forward;
	}

}
